package leetcode.D100.T13;

import java.util.HashMap;
import java.util.Map;

/**
 * @File Info: leetcode -- <Solution>
 * @Author: DYZ
 * @Create: 2021-08-08 22:32:52 星期日
 */

public class Solution {
    public int romanToInt(String s) {
        Map<Character, Integer> map = new HashMap<>();
        map.put('M', 1000);
        map.put('D', 500);
        map.put('C', 100);
        map.put('L', 50);
        map.put('X', 10);
        map.put('V', 5);
        map.put('I', 1);
        int num = 0, j = s.length()-1;
        while(j >= 0) {
            char c = s.charAt(j);
            int value = map.get(c);
            if(j+1 < s.length() && map.get(s.charAt(j+1)) > value) {
                num -= value;
            } else {
                num += value;
            }
            --j;
        }
        return num;
    }
}
